<style lang="less">
	/*@import './international.less';*/
</style>

<template>
	<Select v-model="selectData" :clearable="clearable" :url="url" :params="params" :valueField="valueField" :textField="textField" :filterable="filterable" :disabled="disabled">
		<Option v-for="option in options" :value="option[valueField]" :key="option.id">{{ option[textField] }}</Option>
	</Select>
</template>

<script>
    // datagrid
    import util from '@/libs/util';

    export default {
        data () {
            return {
                name: 'autoSelect',
                options: [],
                selectData: this.model
            }
        },
        methods: {
            selectOption(cnf){
                let data = cnf.data || [];
                let url = cnf.url;
                let params = cnf.params;
                util.post({
                    url: url,
                    params: params,
                    ajaxSuccess(response){
                        let context = response.data;
                        let resData = context.data.total === undefined ? context.data : context.data.list; // 支持分页和不分页的
                        resData.forEach((value) => {
                            data.push(value);
                        });
                    }
                })

            }
        },
        props: {
            model: [Object, String, Number],
            clearable: {
                type: Boolean,
                default(){
                    return true;
                }
            },
            url: String,
            params: Object,
            valueField: {
                type: [Object, Number, String],
                default(){
                    return "id";
                }
            },
            textField: {
                type: [Object, Number, String],
                default(){
                    return "value";
                }
            },
            filterable: {
                type: [Boolean],
                default(){
                    return false;
                }
            },
            disabled: {
                type: [Boolean],
                default(){
                    return false;
                }
            }
        },
        mounted() {
            let me = this;
            me.selectOption({
                url: me.url,
                data: me.options,
                params: me.params
            });
        },
        watch: {
            model(val) {
                this.selectData = val;
            },
            selectData(val){
                this.$emit("update:model", val);
            }
        },
    }
</script>